import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
import commonJs from "@/tool/common.js";
import { Modal } from "ant-design-vue";
Vue.use(Router);
// 登录注册
const register = () => import("./viewsMobile/register/index.vue");
const login = () => import("./viewsMobile/login/index.vue");
const forgetPsw = () => import("./viewsMobile/forgetPsw/index.vue");
const resetPsw = () => import("./viewsMobile/forgetPsw/reset.vue");
const notFound = () => import("./viewsMobile/notFound/index.vue");
const youhua = () => import("./viewsMobile/notFound/youhua.vue");
const index = () => import("./viewsMobile/index/index.vue");
// 首页
const home = () => import("./viewsMobile/home/index.vue");
// 项目中心
const project = () => import("./viewsMobile/project/index.vue");
const projectList = () => import("./viewsMobile/project/project.vue");
const projectDetail = () => import("./viewsMobile/project/detail.vue");
const projectPay = () => import("./viewsMobile/project/pay.vue");
// 新闻中心
const news = () => import("./viewsMobile/new/index.vue");
const newsList = () => import("./viewsMobile/new/new.vue");
const newsDetail = () => import("./viewsMobile/new/detail.vue");
// 个人中心
const person = () => import("./viewsMobile/person/index.vue");
const personHome = () => import("./viewsMobile/person/home.vue");
const personInfo = () => import("./viewsMobile/person/info.vue");
const personFriends = () => import("./viewsMobile/person/friends.vue");
const personBalance = () => import("./viewsMobile/person/balance.vue");
const personBalanceDetail = () =>
  import("./viewsMobile/person/balanceDetail.vue");
const personRecharge = () => import("./viewsMobile/person/recharge.vue");
const personProject = () => import("./viewsMobile/person/project.vue");
const personSearch = () => import("./viewsMobile/person/search.vue");
const personWithdraw = () => import("./viewsMobile/person/withdraw.vue");
const personSetting = () => import("./viewsMobile/person/setting.vue");
const personBank = () => import("./viewsMobile/person/bank.vue");
const personAddCard = () => import("./viewsMobile/person/addCard.vue");
const personResetPayPsw = () => import("./viewsMobile/person/resetPayPsw.vue");
const personResetLoginPsw = () =>
  import("./viewsMobile/person/resetLoginPsw.vue");
const personResetAthPhone = () =>
  import("./viewsMobile/person/resetAthPhone.vue");
const personUnbindingAthPhone = () =>
  import("./viewsMobile/person/unbindingAthPhone.vue");
const personResetLoginPhone = () =>
  import("./viewsMobile/person/resetLoginPhone.vue");
const personResetLoginPhoneNext = () =>
  import("./viewsMobile/person/resetLoginPhoneNext.vue");
const personChangePhone = () => import("./viewsMobile/person/changePhone.vue");
const personContract = () => import("./viewsMobile/person/contract.vue");
const personMessage = () => import("./viewsMobile/person/message.vue");
const personAuthentication = () =>
  import("./viewsMobile/person/authentication.vue");
const questionnaire = () => import("./viewsMobile/person/questionnaire.vue");
// 关于我们
const about = () => import("./viewsMobile/about/index.vue");
const brand = () => import("./viewsMobile/about/brand.vue");
const safe = () => import("./viewsMobile/about/safe.vue");
const invest = () => import("./viewsMobile/about/invest.vue");
const question = () => import("./viewsMobile/about/question.vue");
let router = new Router({
  // mode: "history",
  base: process.env.BASE_URL,
  routes: [
    { path: "/", redirect: "/index" },
    {
      path: "/index",
      component: index,
      name: "index",
      meta: { name: "好融艺" },
      redirect: "/index/home",
      children: [
        {
          path: "home",
          name: "home",
          component: home,
          meta: {
            name: "首页",
            showMenu: true,
            member: true,
            activeHeaderMenu: "home",
            isShowNav: true
          }
        },
        {
          path: "project",
          name: "project",
          component: project,
          redirect: "project/list",
          meta: {
            name: "项目中心",
            needLogin: true,
            member: true,
            showMenu: true,
            needValidate: true,
            activeHeaderMenu: "project"
          },
          children: [
            {
              path: "list",
              name: "projectList",
              component: projectList,
              meta: {
                name: "项目中心",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "project",
                isShowNav: true
              }
            },
            {
              path: "detail/:id",
              name: "projectDetail",
              component: projectDetail,
              meta: {
                name: "项目详情",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "project"
              }
            },
            {
              path: "pay/:id",
              name: "projectPay",
              component: projectPay,
              meta: {
                name: "项目支持",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "project"
              }
            }
          ]
        },
        {
          path: "new",
          name: "new",
          component: news,
          meta: {
            name: "新闻中心",
            showMenu: true,
            activeHeaderMenu: "new"
            // needLogin: true
          },
          redirect: "new/list",
          children: [
            {
              path: "list",
              name: "newsList",
              component: newsList,
              meta: {
                name: "新闻中心",
                activeHeaderMenu: "new",
                // needLogin: true,
                isShowNav: true
              }
            },
            {
              path: "detail/:id",
              name: "newsDetail",
              component: newsDetail,
              meta: {
                name: "新闻详情",
                activeHeaderMenu: "new"
                // needLogin: true
              }
            }
          ]
        },
        {
          path: "person",
          name: "person",
          component: person,
          redirect: "person/home",
          meta: {
            name: "个人中心",
            showMenu: true,
            needLogin: true,
            member: true,
            activeHeaderMenu: "person"
          },
          children: [
            {
              path: "home",
              name: "personHome",
              component: personHome,
              meta: {
                name: "个人首页",
                needLogin: true,
                member: true,
                activeHeaderMenu: "person",
                isShowNav: true
              }
            },
            {
              path: "info",
              name: "personInfo",
              component: personInfo,
              meta: {
                name: "个人资料",
                needLogin: true,
                member: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "balance",
              name: "personBalance",
              component: personBalance,
              meta: {
                name: "余额",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "balance-detail",
              name: "personBalanceDetail",
              component: personBalanceDetail,
              meta: {
                name: "明细",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "project",
              name: "personProject",
              component: personProject,
              meta: {
                name: "项目管理",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "friends",
              name: "personFriends",
              component: personFriends,
              meta: {
                name: "我的好友",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "search",
              name: "personSearch",
              component: personSearch,
              meta: {
                name: "搜索",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "setting",
              name: "personSetting",
              component: personSetting,
              meta: {
                name: "账户设置",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "contract",
              name: "personContract",
              component: personContract,
              meta: {
                name: "合同管理",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "message",
              name: "personMessage",
              component: personMessage,
              meta: {
                name: "消息中心",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "recharge",
              name: "personRecharge",
              component: personRecharge,
              meta: {
                name: "充值",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "pay/:id",
              name: "personPay",
              component: projectPay,
              meta: {
                name: "充值支付",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "withdraw",
              name: "personWithdraw",
              component: personWithdraw,
              meta: {
                name: "提现",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "bank",
              name: "personBank",
              component: personBank,
              meta: {
                name: "银行卡管理",
                needLogin: true,
                member: true,
                needValidate: true,
                needSign: true,
                needMobile: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "add-bank",
              name: "personAddCard",
              component: personAddCard,
              meta: {
                name: "添加银行卡",
                needLogin: true,
                member: true,
                needValidate: true,
                needSign: true,
                needMobile: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "reset-pay-psw",
              name: "personResetPayPsw",
              component: personResetPayPsw,
              meta: {
                name: "重置支付密码",
                needLogin: true,
                member: true,
                needValidate: true,
                needMobile: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "reset-login-psw",
              name: "personResetLoginPsw",
              component: personResetLoginPsw,
              meta: {
                name: "修改登录密码",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "reset-ath-phone",
              name: "personResetAthPhone",
              component: personResetAthPhone,
              meta: {
                name: "修改认证手机",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "unbinding-ath-phone",
              name: "personUnbindingAthPhone",
              component: personUnbindingAthPhone,
              meta: {
                name: "修改认证手机",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "reset-login-phone",
              name: "personResetLoginPhone",
              component: personResetLoginPhone,
              meta: {
                name: "修改登录手机",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "reset-login-phone-next",
              name: "personResetLoginPhoneNext",
              component: personResetLoginPhoneNext,
              meta: {
                name: "修改登录手机",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "change-phone",
              name: "personChangePhone",
              component: personChangePhone,
              meta: {
                name: "实名更绑登录手机",
                needLogin: true,
                member: true,
                needValidate: true,
                activeHeaderMenu: "person"
              }
            },
            {
              path: "authentication",
              name: "personAuthentication",
              component: personAuthentication,
              meta: {
                name: "认证",
                needLogin: true,
                member: true,
                activeHeaderMenu: "person"
              }
            }
          ]
        },
        {
          path: "questionnaire",
          name: "questionnaire",
          component: questionnaire,
          meta: {
            name: "问卷调查",
            needLogin: true,
            activeHeaderMenu: "person"
          }
        },
        {
          path: "about",
          name: "about",
          component: about,
          redirect: "about/brand",
          meta: {
            name: "关于我们",
            showMenu: true,
            activeHeaderMenu: "about"
            // needLogin: true
          },
          children: [
            {
              path: "brand",
              name: "brand",
              component: brand,
              meta: {
                name: "品牌简介",
                showMenu: true,
                activeHeaderMenu: "about",
                // needLogin: true,member: true,
                isShowNav: true
              }
            },
            {
              path: "invest",
              name: "invest",
              component: invest,
              meta: {
                name: "投资指南",
                showMenu: true,
                activeHeaderMenu: "about",
                // needLogin: true,member: true,
                isShowNav: true
              }
            },
            {
              path: "safe",
              name: "safe",
              component: safe,
              meta: {
                name: "安全保障",
                showMenu: true,
                activeHeaderMenu: "about",
                // needLogin: true,member: true,
                isShowNav: true
              }
            },
            {
              path: "question",
              name: "question",
              component: question,
              meta: {
                name: "常见问题",
                showMenu: true,
                activeHeaderMenu: "about",
                // needLogin: true,member: true,
                isShowNav: true
              }
            }
          ]
        }
      ]
    },
    {
      path: "/register",
      name: "register",
      component: register,
      meta: { name: "注册" }
    },

    {
      path: "/login",
      name: "login",
      component: login,
      meta: { name: "登录" }
    },
    {
      path: "/forget",
      name: "forgetPsw",
      component: forgetPsw,
      meta: { name: "忘记密码" }
    },
    {
      path: "/reset",
      name: "resetPsw",
      component: resetPsw,
      meta: { name: "重置密码" }
    },
    {
      path: "/youhua",
      name: "youhua",
      component: youhua,
      meta: { name: "youhua" }
    },
    {
      path: "*",
      name: "notFound",
      component: notFound,
      meta: { name: "404" }
    }
  ]
});
export default router;

router.beforeEach(function(to, from, next) {
  let validate = true;
  let readyPromise = null;
  let readyPromiseRS = null;
  readyPromise = new Promise(resolve => {
    readyPromiseRS = resolve;
  });
  if (to.matched[0].name === "index") {
    // 只在刷新的时候获取sessionStorage的state
    if (from.matched.length === 0) {
      let state = window.sessionStorage.getItem("state");
      if (state) {
        store.replaceState(JSON.parse(state));
      }
      commonJs.getInfo(() => {
        readyPromiseRS();
      });
    } else {
      readyPromiseRS();
    }
  } else {
    readyPromiseRS();
  }
  readyPromise.then(() => {
    if (to.meta.needLogin) {
      if (!store.state.isLogin) {
        validate = false;
        Modal.confirm({
          title: "亲爱的游客：",
          content: "您尚未进行登录，请您前往登录",
          okText: "去登录",
          cancelText: "回首页",
          onOk() {
            router.push({ name: "login" });
          },
          onCancel() {
            router.push({ name: "home" });
          }
        });
      } else {
        if (to.meta.member) {
          if (store.state.memberType === "") {
            validate = false;
            Modal.warning({
              title: "尊敬的用户：",
              content: "您尚未进行风险评估或您的风险评估已过期，请前往评估",
              okText: "前往评估",
              onOk() {
                router.push({ name: "questionnaire" });
              }
            });
          } else {
            if (to.meta.needValidate) {
              if (!store.state.isValidate) {
                validate = false;
                Modal.confirm({
                  title: "尊敬的用户：",
                  content:
                    "您暂无权限查看此页面，请立即前往个人中心，完善身份信息后方可查看！",
                  okText: "去认证",
                  cancelText: "取消",
                  onOk() {
                    router.push({ name: "personAuthentication" });
                  },
                  onCancel() {
                    if (from.matched.length === 0) {
                      router.push({ name: "personHome" });
                    }
                  }
                });
              } else {
                if (to.meta.needMobile) {
                  if (!store.state.isMobile) {
                    validate = false;
                    Modal.confirm({
                      title: "尊敬的用户：",
                      content: "您尚未进行手机认证！",
                      okText: "去认证",
                      cancelText: "取消",
                      onOk() {
                        router.push({ name: "personResetAthPhone" });
                      },
                      onCancel() {
                        if (from.matched.length === 0) {
                          router.push({ name: "personHome" });
                        }
                      }
                    });
                  } else {
                    if (to.meta.needSign) {
                      if (!store.state.isSign) {
                        validate = false;
                        Modal.confirm({
                          title: "尊敬的用户：",
                          content: "您尚未签订《会员电子协议》！",
                          okText: "去查看并签订",
                          cancelText: "取消",
                          onOk() {
                            commonJs.sign();
                          },
                          onCancel() {
                            if (from.matched.length === 0) {
                              router.push({ name: "personHome" });
                            }
                          }
                        });
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    } else {
      if (to.meta.member) {
        if (store.state.memberType === "" && store.state.isLogin) {
          validate = false;
          Modal.warning({
            title: "尊敬的用户：",
            content: "您尚未进行风险评估或您的风险评估已过期，请前往评估",
            okText: "前往评估",
            onOk() {
              router.push({ name: "questionnaire" });
            }
          });
        }
      }
    }
    if (validate) {
      if (to.meta.isShowNav) {
        store.dispatch("changeIsShowNav", to.meta.isShowNav);
      } else {
        store.dispatch("changeIsShowNav", false);
      }
      store.dispatch("changeActiveMenu", to.meta.activeHeaderMenu);
      store.dispatch("changeCurrentRouter", to.name);
    }
    if (validate) {
      next();
    }
  });
});
